package com.srit.map.web.action;

import java.io.PrintWriter;
import java.util.Date;

import org.apache.struts2.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;

import com.srit.core.constant.SystemConstant;
import com.srit.core.struts2.action.CrudAction;
import com.srit.map.business.bean.Company;
import com.srit.map.business.bean.CompanyDescription;
import com.srit.map.business.bean.CompanySearch;
import com.srit.map.business.bean.History;
import com.srit.map.business.bean.SearchBean;
import com.srit.map.business.bean.User;
import com.srit.map.business.service.CompanyService;
import com.srit.map.business.service.HistoryService;

/**
 * @author chenxiling
 *
 */
public class CompanyAction extends CrudAction {
	
	private Company company=new Company();
	@Autowired(required = true)
	private CompanyService companyService;
	@Autowired(required = true)
	private HistoryService historyService;

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ModelDriven#getModel()
	 */
	@Override
	public Object getModel() {
		// TODO Auto-generated method stub
		return company;
	}
	//company详情
	public String getCompanyDesc(){
		int id=Integer.parseInt(request.getParameter("id"));
		CompanySearch comS = new CompanySearch();
		comS.setId(id);
		CompanyDescription dis = companyService.getCompanyDiscription(comS);
		request.setAttribute("company", dis);
		return SUCCESS;
	}
	// 条件搜索
	public String search(){
		SearchBean searchBean = new SearchBean();
		searchBean.setSearchColumn(request.getParameter("searchColumn"));
		searchBean.setSearchKey(request.getParameter("searchKey"));
		page.setPageSize(10);
		companyService.search(searchBean, page);
		return "page";
	}
	//公司详情返回
	public void getComjson(){
		String name = request.getParameter("name");
		CompanySearch comS = new CompanySearch();
		comS.setName(name);
			
		CompanyDescription dis = companyService.getCompanyDiscription(comS);
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out;
		try {
			String str=JSONUtil.serialize(dis);
			out = response.getWriter();
			out.println(str);
			out.flush();
			out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//模糊查找
	public String searchByKey(){
		SearchBean searchBean = new SearchBean();
		//searchBean.setSearchColumn(request.getParameter("searchColumn"));
		searchBean.setSearchKey(request.getParameter("searchKey"));
		page.setPageSize(10);
		page.setSp(searchBean);
		companyService.searchByKey(page);
		return "page";
	}
	
	//update公司详情
	public String updateDes(){
		String name = request.getParameter("name");
		String law_person = request.getParameter("law_person");
		String registered_fund = request.getParameter("registered_fund");
		String company_type = request.getParameter("company_type");
		String business_type = request.getParameter("business_type");
		String address = request.getParameter("address");
		String location = request.getParameter("location");
		String a[] = location.split(",");
		CompanyDescription companyDescription = new CompanyDescription();
		if(name != null){
		companyDescription.setName(name);
		companyDescription.setLaw_person(law_person);
		companyDescription.setRegistered_fund(registered_fund);
		companyDescription.setCompany_type(company_type);
		companyDescription.setBusiness_type(business_type);
		companyDescription.setAddress(address);
		companyDescription.setPost_long(Float.parseFloat(a[0]));
		companyDescription.setPost_lat(Float.parseFloat(a[1]));
		}
		companyService.updateDes(companyDescription);
		return "update";
	}
	
	public void prepareUpdateDes(){
		String name = request.getParameter("name");
		History history = new History();
		history.setOperate("更新"+ name +"信息");
		User user = (User)request.getSession().getValue(SystemConstant.SESSION_USER);
		history.setUser_name(user.getUsername());
		Date operate_date = new Date();
		history.setOperate_date(operate_date);
		historyService.insert(history);
		//插入历史记录
	}
}
